import subprocess


def release_and_renew_ip_powershell():
    try:
        # 使用 PowerShell 释放 IP
        subprocess.run(["powershell", "-Command", "Get-NetAdapter | Reset-NetIPAddress -Confirm:$false"], check=True)
        print("已成功释放 IP 地址")

        # 使用 PowerShell 更新 IP
        subprocess.run(["powershell", "-Command", "Get-NetAdapter | Set-NetIPInterface -DHCP Enabled"], check=True)
        print("已成功重新获取 IP 地址")

    except subprocess.CalledProcessError as e:
        print(f"执行命令时出错: {e}")
    except Exception as e:
        print(f"发生未知错误: {e}")


# 执行函数
release_and_renew_ip_powershell()